package com.ng.desktop.hr.services;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.csvreader.CsvReader;
import com.ng.desktop.hr.model.CaseContactType;
import com.ng.desktop.hr.model.CasePriority;
import com.ng.desktop.hr.model.CaseState;
import com.ng.desktop.hr.model.Cases;
import com.ng.desktop.hr.model.Customer;
import com.ng.desktop.hr.model.Email;
import com.ng.desktop.hr.model.Phone;
import com.ng.desktop.hr.persistence.CasePersistence;
import com.ng.desktop.hr.persistence.CustomerPersistence;
import com.ng.framework.persistence.NGSearchCriteria;
import com.ng.framework.persistence.NGSearchParameter;
import com.ng.framework.persistence.NGSearchParameter.Type;
import com.ng.framework.persistence.NGSearchParameter.UnaryOperation;
import com.ng.framework.util.NGUtil;

public class HRMigrator {

	public static void main(String[] args) {
		migrarCasos(args);
	}

	public static void migrarCasos(String[] args) {
		try {

			CustomerPersistence customerPersistence = (CustomerPersistence) NGFactoryBean.getInstance().getBean(
					"customerPersistence");

			CasePersistence casePersistence = (CasePersistence) NGFactoryBean.getInstance().getBean("casePersistence");

			CsvReader casesCSV = new CsvReader("/home/psabbate/Desktop/cases1.csv");

			casesCSV.readHeaders();

			NGSearchParameter ngSearchParameter;
			NGSearchCriteria criteria;
			Customer customer;
			Cases cases;
			CaseContactType caseContactType = new CaseContactType();
			caseContactType.setId(1L);

			CaseState caseStatePending = new CaseState();
			caseStatePending.setId(1L);

			CaseState caseStateClosed = new CaseState();
			caseStateClosed.setId(3L);

			CasePriority casePriority = new CasePriority();
			casePriority.setId(3L);

			while (casesCSV.readRecord()) {
				String fullname = casesCSV.get("fullname");
				String email = casesCSV.get("email");
				String phone = casesCSV.get("phone");

				String title = casesCSV.get("title");
				String creationDate = casesCSV.get("creationDate");
				String observations = casesCSV.get("observations");
				String pending = casesCSV.get("pending");

				boolean pendingBoolean = pending.toLowerCase().equals("si") ? false : true;

				ngSearchParameter = new NGSearchParameter(Customer.class, "emails", email, Type.STRING, UnaryOperation.LIKE,
						true, "literal");
				criteria = new NGSearchCriteria();
				criteria.addNGSearchParameter(ngSearchParameter);
				List<Customer> all = customerPersistence.getAll(criteria);

				if (all != null && !all.isEmpty()) {
					customer = all.get(0);
					customer.setLastUpdate(new Date());
					customer.setPending(pendingBoolean);

					if (StringUtils.isEmpty(customer.getFullName()) || !customer.getFullName().equals(fullname)) {
						customer.setFullName(fullname);
						customer.buildFirstLastName(fullname, " ");
					}
					if (customer.getPrimaryPhone() == null) {
						Phone customerPhone = new Phone();
						customerPhone.setNumber(phone);
						customerPhone.setPrimaryUse(true);
						customer.getPhones().add(customerPhone);
					}
					if (!customer.getPrimaryPhone().getNumber().equals(phone)) {
						customer.getPrimaryPhone().setNumber(phone);
					}

				} else {
					customer = new Customer();
					customer.setCreationDate(new Date());
					customer.setFullName(fullname);
					customer.buildFirstLastName(fullname, " ");
					customer.setPending(pendingBoolean);
					customer.setLastUpdate(new Date());

					Phone customerPhone = new Phone();
					customerPhone.setNumber(phone);
					customerPhone.setPrimaryUse(true);
					customer.getPhones().add(customerPhone);

					Email customerEmail = new Email();
					customerEmail.setLiteral(email);
					customerEmail.setPrimaryUse(true);
					customer.getEmails().add(customerEmail);

				}

				if (customer.getId() != null) {
					customerPersistence.update(customer);
				} else {
					customerPersistence.create(customer);
				}

				cases = new Cases();
				cases.setTitle(title);
				Date creation;
				try {
					creation = NGUtil.ddMMyyyy_dateFormat.parse(creationDate);
				} catch (ParseException e) {
					creation = new Date();
				}
				cases.setCreationDate(creation);
				cases.setObservations(observations);
				cases.setCaseContactType(caseContactType);
				cases.setOut(false);
				cases.setCasePriority(casePriority);
				cases.setCustomer(customer);

				if (pendingBoolean) {
					cases.setCaseState(caseStatePending);
				} else {
					cases.setCaseState(caseStateClosed);
					cases.setRealResolutionDate(creation);
				}

				casePersistence.create(cases);
			}

			casesCSV.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
